﻿#include "FITKCFDPostSteady.h"
#include "FITKAlgDatReader.h"
#include "FITKAlgPltReader.h"
#include "FITKAlgCGNSReader.h"

#include "FITK_Component/FITKRenderWindowVTK/FITKGraphObjectVTK.h"

#include <vtkDataSetReader.h>
#include <vtkAlgorithm.h>
#include <vtkAlgorithm.h>
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkUnstructuredGrid.h>

#include <QFile>

namespace Interface
{
    FITKCFDPostSteady::FITKCFDPostSteady(QString fileType) :
        _fileType(fileType)
    {
        if (fileType == "vtk") {
            _dataReader = vtkDataSetReader::New();
        }
        else if (fileType == "cgns") {
            _dataReader = FITKAlgCGNSReader::New();
        }
        else if (fileType == "dat") {
            _dataReader = FITKAlgDatReader::New();
        }
        else if (fileType == "plt") {
            _dataReader = FITKAlgPltReader::New();
        }

        if (_dataReader == nullptr)return;
        vtkDataSetMapper* mapper = vtkDataSetMapper::New();
        mapper->SetScalarVisibility(false);
        mapper->SetLookupTable(_lookUpTable);
        mapper->SetInterpolateScalarsBeforeMapping(true);  
        mapper->SetInputConnection(_dataReader->GetOutputPort());
        _mappers.append(mapper);
    }

    FITKCFDPostSteady::~FITKCFDPostSteady()
    {
        if (_dataReader) {
            _dataReader->Delete();
            _dataReader = nullptr;
        }
    }

    FITKPostDataType FITKCFDPostSteady::getPostDataType()
    {
        return FITKPostDataType::Post_Steady;
    }

    void FITKCFDPostSteady::setFile(QString & fileName)
    {
        if (_mappers.isEmpty())return;

        _fileName = fileName;
        if (_fileName.isEmpty())return;
        QByteArray ba = fileName.toLocal8Bit();
        char* cstr = ba.data();

        if (_fileType == "vtk") {
            vtkDataSetReader* dataSetReader = vtkDataSetReader::SafeDownCast(_dataReader);
            if (dataSetReader == nullptr)return;
            dataSetReader->SetFileName(cstr);
            dataSetReader->ReadAllScalarsOn();
            dataSetReader->ReadAllVectorsOn();
            dataSetReader->ReadAllTensorsOn();
            dataSetReader->ReadAllFieldsOn();
            dataSetReader->ReadAllColorScalarsOn();
            dataSetReader->ReadAllNormalsOn();
            dataSetReader->ReadAllTCoordsOn();
        }
        else if (_fileType == "cgns") {
            FITKAlgCGNSReader* datReader = dynamic_cast<FITKAlgCGNSReader*>(_dataReader);
            if (datReader == nullptr)return;
            datReader->setFileName(_fileName);
        }
        else if (_fileType == "dat") {
            FITKAlgDatReader* datReader = dynamic_cast<FITKAlgDatReader*>(_dataReader);
            if (datReader == nullptr)return;
            datReader->setFileName(_fileName);
        }
        else if (_fileType == "plt") {
            FITKAlgPltReader* datReader = dynamic_cast<FITKAlgPltReader*>(_dataReader);
            if (datReader == nullptr)return;
            datReader->setFileName(_fileName);
        }
    }

    vtkDataSet* FITKCFDPostSteady::getOutput()
    {
        if (_dataReader == nullptr)return nullptr;
        if (_fileType == "vtk") {
            vtkDataSetReader* dataSetReader = vtkDataSetReader::SafeDownCast(_dataReader);
            if (dataSetReader == nullptr)return nullptr;
            return dataSetReader->GetOutput();
        }
        else if (_fileType == "cgns") {
            FITKAlgCGNSReader* dataSetReader = FITKAlgCGNSReader::SafeDownCast(_dataReader);
            if (dataSetReader == nullptr)return nullptr;
            return dataSetReader->GetOutput();
        }
        else if (_fileType == "dat") {
            FITKAlgDatReader* datReader = dynamic_cast<FITKAlgDatReader*>(_dataReader);
            if (datReader == nullptr)return nullptr;
            return datReader->GetOutput();
        }
        else if (_fileType == "plt") {
            FITKAlgPltReader* datReader = dynamic_cast<FITKAlgPltReader*>(_dataReader);
            if (datReader == nullptr)return nullptr;
            return datReader->GetOutput();
        }
        return nullptr;
    }
    vtkAlgorithmOutput * FITKCFDPostSteady::getOutputPort()
    {
        if (_dataReader == nullptr)return nullptr;
        return _dataReader->GetOutputPort();
    }
}

